package com.example.xposed_apk.Utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.projection.MediaProjectionManager;
import android.os.Build;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

public class AntiScreenSecure {
    private static final String TAG = "AntiScreenSecure";

    /**
     * 对外静态调用入口，只需传入 lpparam 即可一键绕过 FLAG_SECURE 和录屏授权检查
     */
    public static void bypassScreenSecure(final XC_LoadPackage.LoadPackageParam lpparam) {
        hookActivityWindowFlags(lpparam);
        hookMediaProjection(lpparam);
    }

    /** 移除所有 Activity 窗口的 FLAG_SECURE 标记，允许截屏/录屏 */
    private static void hookActivityWindowFlags(final XC_LoadPackage.LoadPackageParam lpparam) {
        try {
            // Hook setContentView：布局加载后清除 FLAG_SECURE
            XposedHelpers.findAndHookMethod(
                    Activity.class, "setContentView", int.class,
                    new XC_MethodHook() {
                        @Override protected void afterHookedMethod(MethodHookParam param) {
                            Activity act = (Activity) param.thisObject;
                            act.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
                            Log.i(TAG, "清除 FLAG_SECURE on " + act.getLocalClassName());
                        }
                    });

            // Hook onCreate：Activity 创建时清除 FLAG_SECURE
            XposedHelpers.findAndHookMethod(
                    Activity.class, "onCreate", android.os.Bundle.class,
                    new XC_MethodHook() {
                        @Override protected void afterHookedMethod(MethodHookParam param) {
                            Activity act = (Activity) param.thisObject;
                            act.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
                            Log.i(TAG, "onCreate 清除 FLAG_SECURE for " + act.getLocalClassName());
                        }
                    });

            // Hook Window.addFlags：去除 FLAG_SECURE 位
            XposedHelpers.findAndHookMethod(
                    Window.class, "addFlags", int.class,
                    new XC_MethodHook() {
                        @Override protected void beforeHookedMethod(MethodHookParam param) {
                            int flags = (int) param.args[0];
                            param.args[0] = flags & ~WindowManager.LayoutParams.FLAG_SECURE;
                        }
                    });

            // Hook Window.setFlags：同样去除 FLAG_SECURE
            XposedHelpers.findAndHookMethod(
                    Window.class, "setFlags", int.class, int.class,
                    new XC_MethodHook() {
                        @Override protected void beforeHookedMethod(MethodHookParam param) {
                            int flags = (int) param.args[0];
                            param.args[0] = flags & ~WindowManager.LayoutParams.FLAG_SECURE;
                        }
                    });

        } catch (Throwable t) {
            XposedBridge.log(TAG + " hookActivityWindowFlags 出错: " + Log.getStackTraceString(t));
        }
    }

    /** 绕过 MediaProjection 授权检查，伪造录屏 Intent */
    private static void hookMediaProjection(final XC_LoadPackage.LoadPackageParam lpparam) {
        try {
            XposedHelpers.findAndHookMethod(
                    MediaProjectionManager.class, "createScreenCaptureIntent",
                    new XC_MethodHook() {
                        @Override protected void beforeHookedMethod(MethodHookParam param) {
                            Context ctx = (Context) param.thisObject;
                            Intent fake = new Intent();
                            fake.setClassName("com.android.systemui",
                                    "com.android.systemui.screenshot.TakeScreenshotService");
                            param.setResult(fake);
                            Log.i(TAG, "伪造录屏 Intent for " + ctx.getPackageName());
                        }
                    });

        } catch (Throwable t) {
            XposedBridge.log(TAG + " hookMediaProjection 出错: " + Log.getStackTraceString(t));
        }
    }
}
